﻿import json
import time

from easydict import EasyDict as Ed
from numpy import ndarray

from cjc import project_setting

starts = '{', '['
ends = '}', ']'
web_client_name = 'web'
app_client_name = 'app'
app_error_code = 500
app_success_code = 200
app_msg_code = 0
app_verify_code = 1
app_request_msg_code = 100
app_heartbeat_code = 300
app_video_file_finished = 400
front_client_msg_code = 600  # 标记前端消息
front_client_cmd_code = 601  # 标记前端消息
detect_client_msg_code = 700  # 标记检测器消息
detect_client_cmd_code = 701  # 标记检测器消息
ws_server_cmd_code = 800  # 标记检测器消息
app_start_track = 1001
app_stop_track = 1000
ADDR = "127.0.0.1:9000/back_client/yolo"
me = "yolo"

"""数据格式:

    传输到web上的数据格式：
        id, sex,age, -》  男性，年龄 
    1.与前端网页交换数据
        0：普通消息 ，1：验证权限，2:图片消息 ，500: 失败
        接受,code:data{user:}
        发送,code:data
    
    
   2.与python交换数据
        也采用前端一样的格式不单独列出
        {code:2
        data:{
        client: web or app
        hasImg:True
        name:"项目名称+ 时间",
        人数:231,
        img:"234234234234"
        }}
        
    3. python 服务器发送过来的数据保持在queue中，
     data_queue:
      data:{
        name:"项目名称+ 时间",
        人数:231,
        img:"234234234234"
        }},
         data:{
        name:"项目名称+ 时间",
        人数:231,
        img:"234234234234"
        }},
    
"""


def load_json(txt: str):
    if txt.startswith(starts) and txt.endswith(ends):
        js = json.loads(txt)
        return Ed(js)
    return False


def create_simple_success_reply():
    r = Ed()
    r.code = app_success_code
    r.msg = "接受成功"
    return json.dumps(r)


def create_simple_error_reply():
    r = Ed()
    r.code = app_error_code
    r.msg = r"接受成功"
    return json.dumps(r)


def create_heartbeat_msg():
    r = Ed()
    r.code = app_heartbeat_code
    r.project_id = project_setting.project_id
    r.from_whom = me
    r.data = None
    r.msg = r"心跳"
    return json.dumps(r)


def create_backend_cmd_msg(tracking=0):
    r = Ed()
    r.code = detect_client_cmd_code
    r.from_whom = me
    r.data = {"track": tracking}
    r.msg = r"停止录制"
    return json.dumps(r)


def create_video_file_finished_msg(file_name):
    r = Ed()
    r.code = app_video_file_finished
    r.data = {}
    r.data.file_name = file_name
    r.data.time = int(time.time())
    r.msg = r"录制结束，把当前记录存入数据库"
    return json.dumps(r)


def create_msg_to_client(mp):
    r = Ed()
    r.code = detect_client_msg_code
    r.from_whom = me
    r.project_id = project_setting.project_id
    r.msg = r"发给客户端的图片信息"
    if mp is None:
        r.data = None
    else:
        r.data = mp

    return json.dumps(r)


def create_img_request_msg():
    r = Ed()
    r.code = app_request_msg_code
    r.msg = "给我一张图片！"
    return json.dumps(r)


def create_start_trace_msg():
    r = Ed()
    r.code = app_start_track
    r.msg = "请录制图片！"
    return json.dumps(r)


def create_welcome_msg():
    r = Ed()
    r.code = 1
    r.msg = "欢迎登陆成功！"
    return json.dumps(r)


def create_reject_msg():
    r = Ed()
    r.code = -1
    r.msg = "登陆失败！"
    rstr = json.dumps(r)
    print(rstr)
    return rstr


def check_permission(un, pwd):
    return un == "admin" and pwd == "123456"


def dump_json(data: object) -> str:
    return json.dumps(data)
